

<!DOCTYPE html>
<html lang="zh-CN" data-default-color-scheme=auto>



<head>
  <meta charset="UTF-8">
  <link rel="apple-touch-icon" sizes="76x76" href="/img/fluid.png">
  <link rel="icon" href="/img/fluid.png">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=5.0, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
  <meta name="theme-color" content="#2f4154">
  <meta name="author" content="Moe Wang">
  <meta name="keywords" content="">
  
    <meta name="description" content="JS函数的定义函数的定义：函数是计算机编程中非常重要的一个语法结构。那么什么是函数呢？函数指一组可以随时随地运行的代码集合，当他被调用时可重复的去执行这些代码集合。类比数学中的函数：数学中的函数指给定一个数集A,对A施加对应法则f,记作f（A）,得到另一数集B,也就是B&#x3D;f（A）.那么这个关系式就叫函数关系式,简称函数。  函数声明定义函数JS中创建函数有三种方式：  函数声明 函数表达式 函数对">
<meta property="og:type" content="article">
<meta property="og:title" content="JS函数">
<meta property="og:url" content="https://moewang0321.github.io/2019/08/07/2019-08-07-JS%E5%87%BD%E6%95%B0%EF%BC%88%E4%B8%80%EF%BC%89/index.html">
<meta property="og:site_name" content="moe_大杂烩">
<meta property="og:description" content="JS函数的定义函数的定义：函数是计算机编程中非常重要的一个语法结构。那么什么是函数呢？函数指一组可以随时随地运行的代码集合，当他被调用时可重复的去执行这些代码集合。类比数学中的函数：数学中的函数指给定一个数集A,对A施加对应法则f,记作f（A）,得到另一数集B,也就是B&#x3D;f（A）.那么这个关系式就叫函数关系式,简称函数。  函数声明定义函数JS中创建函数有三种方式：  函数声明 函数表达式 函数对">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://moewang0321.github.io/images/alert.png">
<meta property="og:image" content="https://moewang0321.github.io/images/confirm.png">
<meta property="og:image" content="https://moewang0321.github.io/images/prompt.png">
<meta property="article:published_time" content="2019-08-07T03:12:41.000Z">
<meta property="article:modified_time" content="2023-02-14T08:03:06.667Z">
<meta property="article:author" content="Moe Wang">
<meta property="article:tag" content="JavaScript">
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:image" content="https://moewang0321.github.io/images/alert.png">
  
  
  
  <title>JS函数 - moe_大杂烩</title>

  <link  rel="stylesheet" href="https://lib.baomitu.com/twitter-bootstrap/4.6.1/css/bootstrap.min.css" />



  <link  rel="stylesheet" href="https://lib.baomitu.com/github-markdown-css/4.0.0/github-markdown.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/hint.css/2.7.0/hint.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.css" />



<!-- 主题依赖的图标库，不要自行修改 -->
<!-- Do not modify the link that theme dependent icons -->

<link rel="stylesheet" href="//at.alicdn.com/t/font_1749284_hj8rtnfg7um.css">



<link rel="stylesheet" href="//at.alicdn.com/t/font_1736178_lbnruvf0jn.css">


<link  rel="stylesheet" href="/css/main.css" />


  <link id="highlight-css" rel="stylesheet" href="/css/highlight.css" />
  
    <link id="highlight-css-dark" rel="stylesheet" href="/css/highlight-dark.css" />
  




  <script id="fluid-configs">
    var Fluid = window.Fluid || {};
    Fluid.ctx = Object.assign({}, Fluid.ctx)
    var CONFIG = {"hostname":"moewang0321.github.io","root":"/","version":"1.9.4","typing":{"enable":true,"typeSpeed":70,"cursorChar":"_","loop":false,"scope":[]},"anchorjs":{"enable":true,"element":"h1,h2,h3,h4,h5,h6","placement":"left","visible":"hover","icon":""},"progressbar":{"enable":true,"height_px":3,"color":"#29d","options":{"showSpinner":false,"trickleSpeed":100}},"code_language":{"enable":true,"default":"TEXT"},"copy_btn":true,"image_caption":{"enable":true},"image_zoom":{"enable":true,"img_url_replace":["",""]},"toc":{"enable":true,"placement":"right","headingSelector":"h1,h2,h3,h4,h5,h6","collapseDepth":0},"lazyload":{"enable":true,"loading_img":"/img/loading.gif","onlypost":false,"offset_factor":2},"web_analytics":{"enable":false,"follow_dnt":true,"baidu":null,"google":null,"gtag":null,"tencent":{"sid":null,"cid":null},"woyaola":null,"cnzz":null,"leancloud":{"app_id":null,"app_key":null,"server_url":null,"path":"window.location.pathname","ignore_local":false}},"search_path":"/local-search.xml"};

    if (CONFIG.web_analytics.follow_dnt) {
      var dntVal = navigator.doNotTrack || window.doNotTrack || navigator.msDoNotTrack;
      Fluid.ctx.dnt = dntVal && (dntVal.startsWith('1') || dntVal.startsWith('yes') || dntVal.startsWith('on'));
    }
  </script>
  <script  src="/js/utils.js" ></script>
  <script  src="/js/color-schema.js" ></script>
  


  
<meta name="generator" content="Hexo 5.4.2"></head>


<body>
  

  <header>
    

<div class="header-inner" style="height: 70vh;">
  <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand" href="/">
      <strong>moe_大杂烩</strong>
    </a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/">
                <i class="iconfont icon-home-fill"></i>
                <span>首页</span>
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/archives/">
                <i class="iconfont icon-archive-fill"></i>
                <span>归档</span>
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/categories/">
                <i class="iconfont icon-category-fill"></i>
                <span>分类</span>
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/tags/">
                <i class="iconfont icon-tags-fill"></i>
                <span>标签</span>
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/about/">
                <i class="iconfont icon-user-fill"></i>
                <span>关于</span>
              </a>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" target="_self" href="javascript:;" data-toggle="modal" data-target="#modalSearch" aria-label="Search">
              <i class="iconfont icon-search"></i>
            </a>
          </li>
          
        
        
          <li class="nav-item" id="color-toggle-btn">
            <a class="nav-link" target="_self" href="javascript:;" aria-label="Color Toggle">
              <i class="iconfont icon-dark" id="color-toggle-icon"></i>
            </a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

  

<div id="banner" class="banner" parallax=true
     style="background: url('/img/default.png') no-repeat center center; background-size: cover;">
  <div class="full-bg-img">
    <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
      <div class="banner-text text-center fade-in-up">
        <div class="h2">
          
            <span id="subtitle" data-typed-text="JS函数"></span>
          
        </div>

        
          
  <div class="mt-3">
    
    
      <span class="post-meta">
        <i class="iconfont icon-date-fill" aria-hidden="true"></i>
        <time datetime="2019-08-07 11:12" pubdate>
          2019年8月7日 上午
        </time>
      </span>
    
  </div>

  <div class="mt-1">
    
      <span class="post-meta mr-2">
        <i class="iconfont icon-chart"></i>
        
          5.5k 字
        
      </span>
    

    
      <span class="post-meta mr-2">
        <i class="iconfont icon-clock-fill"></i>
        
        
        
          46 分钟
        
      </span>
    

    
    
  </div>


        
      </div>

      
    </div>
  </div>
</div>

</div>

  </header>

  <main>
    
      

<div class="container-fluid nopadding-x">
  <div class="row nomargin-x">
    <div class="side-col d-none d-lg-block col-lg-2">
      

    </div>

    <div class="col-lg-8 nopadding-x-md">
      <div class="container nopadding-x-md" id="board-ctn">
        <div id="board">
          <article class="post-content mx-auto">
            <!-- SEO header -->
            <h1 style="display: none">JS函数</h1>
            
            
              <div class="markdown-body">
                
                <h2 id="JS函数的定义"><a href="#JS函数的定义" class="headerlink" title="JS函数的定义"></a>JS函数的定义</h2><h3 id="函数的定义："><a href="#函数的定义：" class="headerlink" title="函数的定义："></a>函数的定义：</h3><p>函数是计算机编程中非常重要的一个语法结构。那么什么是函数呢？函数指一组可以随时随地运行的代码集合，当他被调用时可重复的去执行这些代码集合。<br>类比数学中的函数：<br>数学中的函数指给定一个数集A,对A施加对应法则f,记作f（A）,得到另一数集B,也就是B=f（A）.那么这个关系式就叫函数关系式,简称函数。</p>
<hr>
<h2 id="函数声明"><a href="#函数声明" class="headerlink" title="函数声明"></a>函数声明</h2><h3 id="定义函数"><a href="#定义函数" class="headerlink" title="定义函数"></a>定义函数</h3><p>JS中创建函数有三种方式：</p>
<ol>
<li>函数声明</li>
<li>函数表达式</li>
<li>函数对象</li>
</ol>
<h4 id="1-函数声明"><a href="#1-函数声明" class="headerlink" title="1.函数声明"></a>1.函数声明</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><code class="hljs javascript"><span class="hljs-keyword">function</span> <span class="hljs-title function_">foo</span>(<span class="hljs-params"></span>)&#123;<br>	<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-number">123</span>);<br>&#125;<br><br><span class="hljs-title function_">foo</span>();<span class="hljs-comment">//调用函数</span><br></code></pre></td></tr></table></figure>

<h4 id="2-函数表达式"><a href="#2-函数表达式" class="headerlink" title="2.函数表达式"></a>2.函数表达式</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><code class="hljs javascript"><span class="hljs-keyword">var</span> foo = <span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) &#123;<br>	<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-number">123</span>);<br>&#125;<br><br><span class="hljs-title function_">foo</span>();<br></code></pre></td></tr></table></figure>

<h4 id="3-函数对象（了解）"><a href="#3-函数对象（了解）" class="headerlink" title="3.函数对象（了解）"></a>3.函数对象（了解）</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><code class="hljs javascript"><span class="hljs-keyword">var</span> foo = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Function</span>();<br><span class="hljs-title function_">foo</span>();<br></code></pre></td></tr></table></figure>

<h3 id="关于函数声明及解析"><a href="#关于函数声明及解析" class="headerlink" title="关于函数声明及解析"></a>关于函数声明及解析</h3><h4 id="1-函数声明会预解析（仅在当前所在的script块内）"><a href="#1-函数声明会预解析（仅在当前所在的script块内）" class="headerlink" title="1.函数声明会预解析（仅在当前所在的script块内）"></a>1.函数声明会预解析（仅在当前所在的script块内）</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><code class="hljs javascript"><span class="hljs-title function_">foo</span>();	<span class="hljs-comment">//控制台打印123</span><br><br><span class="hljs-keyword">function</span> <span class="hljs-title function_">foo</span>(<span class="hljs-params"></span>)&#123;<br>	<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-number">123</span>);<br>&#125;<br></code></pre></td></tr></table></figure>
<h4 id="2-函数表达式不具备预解析功能"><a href="#2-函数表达式不具备预解析功能" class="headerlink" title="2.函数表达式不具备预解析功能"></a>2.函数表达式不具备预解析功能</h4><h4 id="3-变量解析要高于函数的预解析"><a href="#3-变量解析要高于函数的预解析" class="headerlink" title="3.变量解析要高于函数的预解析"></a>3.变量解析要高于函数的预解析</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><code class="hljs javascript"><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>( a );<span class="hljs-comment">//控制台打印 f a ( )	&#123; &#125;</span><br><span class="hljs-keyword">function</span> <span class="hljs-title function_">a</span>(<span class="hljs-params"></span>)&#123;<br><br>&#125;<br><span class="hljs-keyword">var</span> a = <span class="hljs-number">10</span>;   <span class="hljs-comment">// 变量预解析要高于函数的预解析</span><br></code></pre></td></tr></table></figure>

<h4 id="4-函数表达式可直接调用执行"><a href="#4-函数表达式可直接调用执行" class="headerlink" title="4.函数表达式可直接调用执行"></a>4.函数表达式可直接调用执行</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><code class="hljs javascript"><span class="hljs-keyword">var</span> foo = <span class="hljs-keyword">function</span>(<span class="hljs-params"></span>)&#123;  <span class="hljs-comment">//函数表达式可直接调用执行</span><br>	<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-number">123</span>);<br>	<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-number">456</span>);<br>	<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-number">789</span>);<br>&#125;();<br></code></pre></td></tr></table></figure>
<p>但函数声明是不能直接调用的</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><code class="hljs javascript"><span class="hljs-keyword">function</span> <span class="hljs-title function_">foo</span>(<span class="hljs-params"></span>)&#123;   <span class="hljs-comment">//函数声明是不能直接调用的</span><br>	<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-number">123</span>);<br>	<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-number">456</span>);<br>	<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-number">789</span>);<br>&#125;();<br></code></pre></td></tr></table></figure>
<h3 id="关于函数变量的区域性"><a href="#关于函数变量的区域性" class="headerlink" title="关于函数变量的区域性"></a>关于函数变量的区域性</h3><p>程序寻找参数的时候只能够寻找和它同级或外层的变量，无法向内层找变量</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><code class="hljs javascript"><span class="hljs-keyword">function</span> <span class="hljs-title function_">foo</span>(<span class="hljs-params"></span>)&#123;<br>	<span class="hljs-keyword">var</span> b = <span class="hljs-number">20</span>;<br>&#125;<br><span class="hljs-keyword">function</span> <span class="hljs-title function_">baz</span>(<span class="hljs-params"></span>)&#123;<br>	<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>( b ); <br>&#125;<br><span class="hljs-title function_">foo</span>();<br><span class="hljs-title function_">baz</span>();<span class="hljs-comment">// =&gt;报错，找不到b </span><br><br><span class="hljs-keyword">var</span> b;<br><span class="hljs-keyword">function</span> <span class="hljs-title function_">foo</span>(<span class="hljs-params"></span>)&#123;<br>	<span class="hljs-keyword">var</span> b = <span class="hljs-number">20</span>;<br>&#125;<br><span class="hljs-keyword">function</span> <span class="hljs-title function_">baz</span>(<span class="hljs-params"></span>)&#123;<br>	<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>( b ); <br>&#125;<br><span class="hljs-title function_">foo</span>();<span class="hljs-comment">// =&gt;b重新赋值 b = 20;</span><br><span class="hljs-title function_">baz</span>();<span class="hljs-comment">// =&gt;打印 20</span><br></code></pre></td></tr></table></figure>

<hr>
<h2 id="变量提升问题"><a href="#变量提升问题" class="headerlink" title="变量提升问题"></a>变量提升问题</h2><p>先甩一道经典变量提升题</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><code class="hljs javascript"><span class="hljs-keyword">var</span> a = <span class="hljs-number">10</span>;<br><span class="hljs-keyword">function</span> <span class="hljs-title function_">foo</span>(<span class="hljs-params"></span>) &#123;<br>    <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(a); <span class="hljs-comment">// ??</span><br>    <span class="hljs-keyword">var</span> a = <span class="hljs-number">20</span>;<br>&#125;<br><span class="hljs-title function_">foo</span>();<br></code></pre></td></tr></table></figure>
<p>问控制台打印的结果是多少？</p>
<p>思考</p>
<p>答案   ：   <code>undefined</code><br>使用<code>var</code>关键字声明的变量在JavaScript中会被提升，并在内存中分配<code>undefined</code>，但初始化却发生在你给变量赋值的地方，另外<code>var</code>声明的变量是<code>函数作用域的</code>，所以上述代码块解析可以看做以下过程</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><code class="hljs javascript"><span class="hljs-keyword">var</span> a = <span class="hljs-number">10</span>; <span class="hljs-comment">// 全局使用域</span><br><span class="hljs-keyword">function</span> <span class="hljs-title function_">foo</span>(<span class="hljs-params"></span>) &#123;<br><span class="hljs-comment">// var a 的声明将被提升到到函数的顶部。</span><br><span class="hljs-comment">// 比如:var a</span><br><br><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(a); <span class="hljs-comment">// 打印 undefined</span><br><br><span class="hljs-comment">// 实际初始化值20只发生在这里</span><br>   <span class="hljs-keyword">var</span> a = <span class="hljs-number">20</span>; <span class="hljs-comment">// local scope</span><br>&#125;<br></code></pre></td></tr></table></figure>

<p>上边的代码块理解了下边的代码块理解起来就很方便了</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><code class="hljs javascript"><span class="hljs-keyword">var</span> a = <span class="hljs-number">18</span>;<br><span class="hljs-title function_">f1</span>();<br><span class="hljs-keyword">function</span> <span class="hljs-title function_">f1</span>(<span class="hljs-params"></span>)&#123;<br>   <span class="hljs-keyword">var</span> b=<span class="hljs-number">9</span>;<br>   <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(a);<br>   <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(b);<br>   <span class="hljs-keyword">var</span> a = <span class="hljs-string">&#x27;123&#x27;</span>;<br>&#125;<br></code></pre></td></tr></table></figure>

<p>答案 ： <code>undefined</code>        ,    <code>9</code><br>解析过程直接以代码形式呈现，看起来比较容易理解</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><code class="hljs javascript"><span class="hljs-comment">//第一步：预解析</span><br><span class="hljs-keyword">var</span> a;<br><span class="hljs-comment">//第三步：当执行函数时，再次进行预解析(局部作用域)</span><br><span class="hljs-keyword">function</span> <span class="hljs-title function_">f1</span>(<span class="hljs-params"></span>)&#123;<br>	<span class="hljs-keyword">var</span> b;<br>	<span class="hljs-keyword">var</span> a;<br>	<span class="hljs-comment">//第四步：一行一行执行代码	</span><br>	b=<span class="hljs-number">9</span>;<br>	<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(a);<br>	<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(b);<br>	a = <span class="hljs-string">&#x27;123&#x27;</span>;<br>&#125;<br><span class="hljs-comment">//第二步：一行一行执行代码</span><br>a = <span class="hljs-number">18</span>;<br><span class="hljs-title function_">f1</span>();<br></code></pre></td></tr></table></figure>

<hr>
<h2 id="JS函数参数"><a href="#JS函数参数" class="headerlink" title="JS函数参数"></a>JS函数参数</h2><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><code class="hljs javascript"><span class="hljs-keyword">function</span> <span class="hljs-title function_">foo</span>(<span class="hljs-params">n</span>)&#123;  <span class="hljs-comment">//形式参数：形参   var n = 123</span><br>	<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(n);<br>&#125;<br><br><span class="hljs-title function_">foo</span>(<span class="hljs-number">123</span>);  <span class="hljs-comment">//实际参数：实参 */</span><br></code></pre></td></tr></table></figure>

<p>实参的个数和形参的个数不一定要一样</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><code class="hljs javascript"><span class="hljs-keyword">function</span> <span class="hljs-title function_">foo</span>(<span class="hljs-params">n1,n2,n3</span>)&#123;  <br>	<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(n3);<br>&#125;<br><br><span class="hljs-title function_">foo</span>(<span class="hljs-string">&#x27;a&#x27;</span>,<span class="hljs-string">&#x27;b&#x27;</span>);<br></code></pre></td></tr></table></figure>

<p><code>arguments</code>对象：表示实参的集合  </p>
<p><code>arguments.length</code>  : 获取集合长度  </p>
<p>通过    <code>[]</code>    获取集合数组的下标</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><code class="hljs javascript"> <span class="hljs-keyword">function</span> <span class="hljs-title function_">foo</span>(<span class="hljs-params"></span>)&#123;  <br>	<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-variable language_">arguments</span>.<span class="hljs-property">length</span>);<br>	<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-variable language_">arguments</span>[<span class="hljs-number">0</span>]); <br>	<span class="hljs-keyword">var</span> result = <span class="hljs-number">0</span>;<br>	<span class="hljs-keyword">for</span>(<span class="hljs-keyword">var</span> i=<span class="hljs-number">0</span>;i&lt;<span class="hljs-variable language_">arguments</span>.<span class="hljs-property">length</span>;i++)&#123;<br>		 result += <span class="hljs-variable language_">arguments</span>[i];    <br>	&#125;<br>	<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>( result );<br>&#125;<br><br><span class="hljs-title function_">foo</span>(<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</span>,<span class="hljs-number">4</span>,<span class="hljs-number">5</span>);<br></code></pre></td></tr></table></figure>

<h2 id="关于JS中函数的参数"><a href="#关于JS中函数的参数" class="headerlink" title="关于JS中函数的参数"></a>关于JS中函数的参数</h2><p><a target="_blank" rel="noopener" href="https://www.cnblogs.com/lcq1467/p/7101519.html">得益于此文的启发</a>  </p>
<p>函数的参数属于函数的内部变量，外部无法访问到，即使和外部变量同名，它们也是两个不同的变量</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><code class="hljs javascript"><span class="hljs-keyword">var</span> a = <span class="hljs-number">100</span>;<br><br><span class="hljs-keyword">function</span> <span class="hljs-title function_">test</span>(<span class="hljs-params">a</span>)&#123;<br>a++;　　　　　　<span class="hljs-comment">//a(形参)是局部变量</span><br><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(a);<br>&#125;<br><span class="hljs-title function_">test</span>(a);<br><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(a); <span class="hljs-comment">//结果是 101 100;</span><br></code></pre></td></tr></table></figure>

<p>类似于：</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><code class="hljs javascript"><span class="hljs-keyword">var</span> a = <span class="hljs-number">100</span>;<br><span class="hljs-keyword">function</span> <span class="hljs-title function_">test</span>(<span class="hljs-params"> </span>)&#123;<br><span class="hljs-keyword">var</span> a=<span class="hljs-number">100</span>;   <span class="hljs-comment">//a是局部变量</span><br>a++;<br><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(a);<br>&#125;<br><span class="hljs-title function_">test</span>( );<br><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(a); <span class="hljs-comment">//结果 101 100;</span><br></code></pre></td></tr></table></figure>

<p>当函数没有定义参数或者内部没有<code>var</code>变量，内部的变量默认为全局变量</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><code class="hljs javascript"><span class="hljs-keyword">var</span> a =<span class="hljs-number">100</span>;<br><span class="hljs-keyword">function</span> <span class="hljs-title function_">test</span> ()&#123;<br>	a++;<br>	<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(a);<br>&#125;<br><span class="hljs-title function_">test</span>();<br><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(a);<span class="hljs-comment">//控制台打印 101	101	，因为a是全局变量</span><br></code></pre></td></tr></table></figure>

<p>当函数定义了参数，但没有传实参的时候，形参默认为undefined</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><code class="hljs javascript"><span class="hljs-keyword">var</span> a = <span class="hljs-number">100</span>;<br><span class="hljs-keyword">function</span> <span class="hljs-title function_">test</span>(<span class="hljs-params">a</span>)&#123;<br>	a++;<br>	<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>( a );<br>&#125;<br><span class="hljs-title function_">test</span>();<br><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>( a );<span class="hljs-comment">//第一个返回undefined	第二个返回100</span><br></code></pre></td></tr></table></figure>

<p>但是如果通过对象传递参数，函数内部对象属性值发生改变，外部被传递对象的属性值也会改变</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><code class="hljs javascript"><span class="hljs-keyword">var</span> a = &#123;x : <span class="hljs-number">100</span>&#125;;<br><span class="hljs-keyword">function</span> <span class="hljs-title function_">test</span>(<span class="hljs-params">a</span>)&#123;<br>	a.<span class="hljs-property">x</span>++;<br>	<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>( a.<span class="hljs-property">x</span> );<br>&#125;<br><span class="hljs-title function_">test</span>(a);<br><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>( a.<span class="hljs-property">x</span> );<span class="hljs-comment">//第一个返回101，第二个返回101</span><br></code></pre></td></tr></table></figure>

<p><strong>总结：</strong><br><code>通过值传递参数</code>：    在函数中调用的参数是函数参数（属于局部变量范畴），如果函数修改参数值，将不会改变外部被引用的实参值<br><code>通过对象传递参数</code>：对象属性的变量是相当于指针，因此函数修改对象的属性值时，外部的对象属性也会改变。<br>这也涉及到基本类型和引用类型的值，基本数据类型（undefined、null、boolean、Number）是按值访问的，所以局部变量与全局变量即使名字一样，也是两个不同的变量<br><code>总而言之</code><br>1.函数参数是被当做变量对待的<br>2.参数是按值传递的<br>3.传递参数的时候要确定传递的数据类型</p>
<p>对象属于引用类型，当变量是一个对象时，将这个变量赋给另一个变量时，相当于另一个变量也变为了指针；两个变量都指向同一对象；所以在引用数据类型照片中，变量相当于一个指针^[注：JavaScript高级程序设计第四章 变量、作用域和内存 有详细介绍]</p>
<hr>
<h2 id="return"><a href="#return" class="headerlink" title="return"></a>return</h2><p>返回值<code>return</code> ： 表示函数执行完的返回值，当函数没有写<code>return</code>的时候，函数调用完就会变成<code>undefined</code>然后<code>return</code>;<br><strong><code>return</code> 类似于跳出操作，所以return后的代码是不执行的</strong></p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><code class="hljs javascript"><span class="hljs-keyword">function</span> <span class="hljs-title function_">foo</span>(<span class="hljs-params"></span>)&#123;<br><br><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-number">123</span>);<br><span class="hljs-keyword">return</span> <span class="hljs-number">456</span>;<br><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-number">789</span>);<span class="hljs-comment">//return后面的代码不执行</span><br>&#125;   <br><br><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>( <span class="hljs-title function_">foo</span>() );<span class="hljs-comment">// 控制台打印 123 456</span><br><br></code></pre></td></tr></table></figure>

<hr>
<p><strong>函数作用域和作用域链会单写一篇</strong></p>
<hr>
<h2 id="函数与事件"><a href="#函数与事件" class="headerlink" title="函数与事件"></a>函数与事件</h2><h3 id="JS事件"><a href="#JS事件" class="headerlink" title="JS事件"></a>JS事件</h3><p>鼠标、键盘触发的一些功能（一些功能就是自己定义的函数，跟事件关联的函数叫做事件函数）</p>
<h3 id="事件函数"><a href="#事件函数" class="headerlink" title="事件函数"></a>事件函数</h3><p><code>onclick</code> : 点击（不只是鼠标，键盘也可以）<br><code>ondbclick</code> : 双击<br><code>onmousedown</code> ：鼠标按下<br><code>onmouseup</code>：鼠标抬起<br><code>onmouseover</code>：鼠标移入<br><code>onmouseout</code>：鼠标移出<br><code>onmousemove</code>：鼠标移动（连续触发）  </p>
<hr>
<h2 id="代码简化"><a href="#代码简化" class="headerlink" title="代码简化"></a>代码简化</h2><p>举个栗子，以前我们获取元素的时候，需要下边这么多的代码</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><code class="hljs javascript"><span class="hljs-keyword">var</span> elem1 = <span class="hljs-variable language_">document</span>.<span class="hljs-title function_">getElementById</span>(<span class="hljs-string">&#x27;elem1&#x27;</span>);<br><span class="hljs-keyword">var</span> elem2 = <span class="hljs-variable language_">document</span>.<span class="hljs-title function_">getElementById</span>(<span class="hljs-string">&#x27;elem2&#x27;</span>);<br><span class="hljs-keyword">var</span> elem3 = <span class="hljs-variable language_">document</span>.<span class="hljs-title function_">getElementById</span>(<span class="hljs-string">&#x27;elem3&#x27;</span>);<br><span class="hljs-keyword">var</span> elem4 = <span class="hljs-variable language_">document</span>.<span class="hljs-title function_">getElementById</span>(<span class="hljs-string">&#x27;elem4&#x27;</span>);<br></code></pre></td></tr></table></figure>

<p>而如果我们把它封装到一个函数里，每次取元素的时候调用，不是方便多了</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><code class="hljs javascript"><span class="hljs-keyword">var</span> elem1 = $(<span class="hljs-string">&#x27;elem1&#x27;</span>);<br><span class="hljs-keyword">var</span> elem2 = $(<span class="hljs-string">&#x27;elem2&#x27;</span>);<br><span class="hljs-keyword">var</span> elem3 = $(<span class="hljs-string">&#x27;elem3&#x27;</span>);<br><span class="hljs-keyword">var</span> elem4 = $(<span class="hljs-string">&#x27;elem4&#x27;</span>);<br><br><span class="hljs-keyword">function</span> <span class="hljs-title function_">$</span>(<span class="hljs-params">id</span>)&#123;<br>	<span class="hljs-keyword">return</span> <span class="hljs-variable language_">document</span>.<span class="hljs-title function_">getElementById</span>(id);<br>&#125;<br></code></pre></td></tr></table></figure>

<hr>
<h2 id="常用内置函数"><a href="#常用内置函数" class="headerlink" title="常用内置函数"></a>常用内置函数</h2><h3 id="alert"><a href="#alert" class="headerlink" title="alert()"></a>alert()</h3><p>提示框</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs javascript"><span class="hljs-title function_">alert</span>(<span class="hljs-number">123</span>);<br></code></pre></td></tr></table></figure>
<p><img src="/images/alert.png" srcset="/img/loading.gif" lazyload alt="alert"></p>
<h3 id="confirm"><a href="#confirm" class="headerlink" title="confirm()"></a>confirm()</h3><p>确定对话框</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs javascript"><span class="hljs-title function_">confirm</span>(<span class="hljs-string">&#x27;你好&#x27;</span>);<br></code></pre></td></tr></table></figure>
<p><img src="/images/confirm.png" srcset="/img/loading.gif" lazyload alt="confirm"></p>
<h3 id="prompt"><a href="#prompt" class="headerlink" title="prompt();"></a>prompt();</h3><p>输入对话框，返回值就是输入的内容</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><code class="hljs javascript"><span class="hljs-keyword">var</span> result = <span class="hljs-title function_">prompt</span>(<span class="hljs-string">&#x27;我叫什么&#x27;</span> , <span class="hljs-string">&#x27;请输入名字&#x27;</span>);<br><span class="hljs-keyword">if</span>(result == <span class="hljs-string">&#x27;xiaoming&#x27;</span>)&#123;<br>	<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">&#x27;添加好友&#x27;</span>);<br>&#125;<br><span class="hljs-keyword">else</span>&#123;<br>	<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">&#x27;拒绝添加&#x27;</span>);<br>&#125;<br></code></pre></td></tr></table></figure>

<p><img src="/images/prompt.png" srcset="/img/loading.gif" lazyload alt="enter description here"></p>
<h3 id="转换数字"><a href="#转换数字" class="headerlink" title="转换数字"></a>转换数字</h3><p>这里简述两种方法<code>parseInt()</code>，<code>parseFloat()</code></p>
<h4 id="parseInt"><a href="#parseInt" class="headerlink" title="parseInt()"></a>parseInt()</h4><p>将字符串转换成整数</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><code class="hljs javascript"><span class="hljs-keyword">var</span> str = <span class="hljs-string">&#x27;123px&#x27;</span>;<br><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>( <span class="hljs-built_in">parseInt</span>(str) );<span class="hljs-comment">//控制台打印  123</span><br></code></pre></td></tr></table></figure>
<p><code>parseInt()</code>会从左至右挨个转换直到不能转换为止</p>
<h4 id="parseFloat"><a href="#parseFloat" class="headerlink" title="parseFloat()"></a>parseFloat()</h4><p>将字符串转换成浮点数</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><code class="hljs javascript"><span class="hljs-keyword">var</span> str = <span class="hljs-string">&#x27;123.45&#x27;</span>;<br><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>( <span class="hljs-built_in">parseFloat</span>(str) );<br></code></pre></td></tr></table></figure>
<h3 id="isNaN"><a href="#isNaN" class="headerlink" title="isNaN()"></a>isNaN()</h3><p>判断是否是NaN，是返回<code>true</code>，不是返回<code>false</code><br>但是因为JS历史原因……还是有BUG的</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs javascript"><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>( <span class="hljs-built_in">isNaN</span>(<span class="hljs-string">&#x27;hello&#x27;</span>) );<span class="hljs-comment">//控制台打印 true </span><br></code></pre></td></tr></table></figure>
<p>很显然 hello 类型是字符串，不是NaN，但是为什么会返回true呢<br>看一眼    w3cschool对isNaN返回值的定义：  </p>
<blockquote>
<p>如果 x 是特殊的非数字值 NaN（或者能被转换为这样的值），返回的值就是 true。如果 x 是其他值,则返回 false。</p>
</blockquote>
<p>也就是说程序内部会首先把hello转换成Number类型，自然就变成了NaN，然后再进行isNaN的判定。<br>ES6提供了一种更为严谨的判断是否是NaN的方法：<code>Number.isNaN()</code></p>
<p>现在用这个新方法来判断一下hello是不是NaN</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs javascript"><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>( <span class="hljs-title class_">Number</span>.<span class="hljs-built_in">isNaN</span>(<span class="hljs-string">&#x27;hello&#x27;</span>) );<span class="hljs-comment">//输出false</span><br></code></pre></td></tr></table></figure>

<h3 id="isFinite"><a href="#isFinite" class="headerlink" title="isFinite()"></a>isFinite()</h3><p>判断是否是无限数字，如果是有限数字返回<code>true</code>，如果不是则返回<code>false</code></p>
<p>Infinity：无限大</p>
<p>-Infinity：无限小</p>
<hr>
<h2 id="Math对象"><a href="#Math对象" class="headerlink" title="Math对象"></a>Math对象</h2><p><code>Math.min()</code>：取最小值<br><code>Math.max()</code>：取最大值<br><code>Math.cell()</code>：向上取整<br><code>Math.floor()</code>：向下取整<br><code>Math.pow()</code>：平方<br><code>Math.sqrt()</code>：开平方<br><code>Math.abs()</code>：取绝对值<br><code>Math.PI</code>：π值<br><code>Math.random()</code>：随机一个    [0,1)     区间的数<br><code>Math.round()</code>：四舍五入</p>
<blockquote>
<p>如果我想要开三次方或者多次方怎么办</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs javascript">&gt;<span class="hljs-title class_">Math</span>.<span class="hljs-title function_">pow</span>(<span class="hljs-number">8</span>,<span class="hljs-number">1</span>/<span class="hljs-number">3</span>);<span class="hljs-comment">//开8的三次方</span><br></code></pre></td></tr></table></figure>
<p>举一反三</p>
</blockquote>
<h2 id="初探this关键字"><a href="#初探this关键字" class="headerlink" title="初探this关键字"></a>初探this关键字</h2><p>这里只介绍this最简单的指向，即<code>this</code>会指向当前事件所操作的元素</p>
<hr>

                
              </div>
            
            <hr/>
            <div>
              <div class="post-metas my-3">
  
    <div class="post-meta mr-3 d-flex align-items-center">
      <i class="iconfont icon-category"></i>
      

<span class="category-chains">
  
  
    
      <span class="category-chain">
        
  <a href="/categories/study/" class="category-chain-item">study</a>
  
  

      </span>
    
  
</span>

    </div>
  
  
    <div class="post-meta">
      <i class="iconfont icon-tags"></i>
      
        <a href="/tags/JavaScript/">#JavaScript</a>
      
    </div>
  
</div>


              
  

  <div class="license-box my-3">
    <div class="license-title">
      <div>JS函数</div>
      <div>https://moewang0321.github.io/2019/08/07/2019-08-07-JS函数（一）/</div>
    </div>
    <div class="license-meta">
      
        <div class="license-meta-item">
          <div>作者</div>
          <div>Moe Wang</div>
        </div>
      
      
        <div class="license-meta-item license-meta-date">
          <div>发布于</div>
          <div>2019年8月7日</div>
        </div>
      
      
      
        <div class="license-meta-item">
          <div>许可协议</div>
          <div>
            
              
              
                <a target="_blank" href="https://creativecommons.org/licenses/by/4.0/">
                  <span class="hint--top hint--rounded" aria-label="BY - 署名">
                    <i class="iconfont icon-by"></i>
                  </span>
                </a>
              
            
          </div>
        </div>
      
    </div>
    <div class="license-icon iconfont"></div>
  </div>



              
                <div class="post-prevnext my-3">
                  <article class="post-prev col-6">
                    
                    
                      <a href="/2019/08/08/2019-08-08-%E7%BB%83%E4%B9%A0/" title="Weekly Practice">
                        <i class="iconfont icon-arrowleft"></i>
                        <span class="hidden-mobile">Weekly Practice</span>
                        <span class="visible-mobile">上一篇</span>
                      </a>
                    
                  </article>
                  <article class="post-next col-6">
                    
                    
                      <a href="/2019/08/06/2019-08-06-%E5%88%9D%E5%85%A5js%EF%BC%88%E4%B8%89%EF%BC%89/" title="初入JS（三）">
                        <span class="hidden-mobile">初入JS（三）</span>
                        <span class="visible-mobile">下一篇</span>
                        <i class="iconfont icon-arrowright"></i>
                      </a>
                    
                  </article>
                </div>
              
            </div>

            
  
  
    <article id="comments" lazyload>
      
    <div id="giscus" class="giscus"></div>
    <script type="text/javascript">
      Fluid.utils.loadComments('#giscus', function() {
        var options = {"repo":"moewang0321/moewang0321.github.io","repo-id":"MDEwOlJlcG9zaXRvcnkyMDEyNDI0NTY=","category":"Show and tell","category-id":"DIC_kwDOC_63WM4CUMsH","theme-light":"light_protanopia","theme-dark":"dark_dimmed","mapping":"pathname","reactions-enabled":1,"emit-metadata":1,"input-position":"top","loading":"lazy","lang":"zh-CN"};
        var attributes = {};
        for (let option in options) {
          if (!option.startsWith('theme-')) {
            var key = option.startsWith('data-') ? option : 'data-' + option;
            attributes[key] = options[option];
          }
        }
        var light = 'light_protanopia';
        var dark = 'dark_dimmed';
        window.GiscusThemeLight = light;
        window.GiscusThemeDark = dark;
        attributes['data-theme'] = document.documentElement.getAttribute('data-user-color-scheme') === 'dark' ? dark : light;
        for (let attribute in attributes) {
          var value = attributes[attribute];
          if (value === undefined || value === null || value === '') {
            delete attributes[attribute];
          }
        }
        var s = document.createElement('script');
        s.setAttribute('src', 'https://giscus.app/client.js');
        s.setAttribute('crossorigin', 'anonymous');
        for (let attribute in attributes) {
          s.setAttribute(attribute, attributes[attribute]);
        }
        var ss = document.getElementsByTagName('script');
        var e = ss.length > 0 ? ss[ss.length - 1] : document.head || document.documentElement;
        e.parentNode.insertBefore(s, e.nextSibling);
      });
    </script>
    <noscript>Please enable JavaScript to view the comments</noscript>


    </article>
  


          </article>
        </div>
      </div>
    </div>

    <div class="side-col d-none d-lg-block col-lg-2">
      
  <aside class="sidebar" style="margin-left: -1rem">
    <div id="toc">
  <p class="toc-header">
    <i class="iconfont icon-list"></i>
    <span>目录</span>
  </p>
  <div class="toc-body" id="toc-body"></div>
</div>



  </aside>


    </div>
  </div>
</div>





  



  



  



  



  







    

    
      <a id="scroll-top-button" aria-label="TOP" href="#" role="button">
        <i class="iconfont icon-arrowup" aria-hidden="true"></i>
      </a>
    

    
      <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">搜索</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v" for="local-search-input">关键词</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>

    

    
  </main>

  <footer>
    <div class="footer-inner">
  
    <div class="footer-content">
       <a href="https://hexo.io" target="_blank" rel="nofollow noopener"><span>Hexo</span></a> <i class="iconfont icon-love"></i> <a href="https://github.com/fluid-dev/hexo-theme-fluid" target="_blank" rel="nofollow noopener"><span>Fluid</span></a> 
    </div>
  
  
  
  
</div>

  </footer>

  <!-- Scripts -->
  
  <script  src="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.js" ></script>
  <link  rel="stylesheet" href="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.css" />

  <script>
    NProgress.configure({"showSpinner":false,"trickleSpeed":100})
    NProgress.start()
    window.addEventListener('load', function() {
      NProgress.done();
    })
  </script>


<script  src="https://lib.baomitu.com/jquery/3.6.0/jquery.min.js" ></script>
<script  src="https://lib.baomitu.com/twitter-bootstrap/4.6.1/js/bootstrap.min.js" ></script>
<script  src="/js/events.js" ></script>
<script  src="/js/plugins.js" ></script>


  <script  src="https://lib.baomitu.com/typed.js/2.0.12/typed.min.js" ></script>
  <script>
    (function (window, document) {
      var typing = Fluid.plugins.typing;
      var subtitle = document.getElementById('subtitle');
      if (!subtitle || !typing) {
        return;
      }
      var text = subtitle.getAttribute('data-typed-text');
      
        typing(text);
      
    })(window, document);
  </script>




  
    <script  src="/js/img-lazyload.js" ></script>
  




  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/tocbot/4.18.2/tocbot.min.js', function() {
    var toc = jQuery('#toc');
    if (toc.length === 0 || !window.tocbot) { return; }
    var boardCtn = jQuery('#board-ctn');
    var boardTop = boardCtn.offset().top;

    window.tocbot.init(Object.assign({
      tocSelector     : '#toc-body',
      contentSelector : '.markdown-body',
      linkClass       : 'tocbot-link',
      activeLinkClass : 'tocbot-active-link',
      listClass       : 'tocbot-list',
      isCollapsedClass: 'tocbot-is-collapsed',
      collapsibleClass: 'tocbot-is-collapsible',
      scrollSmooth    : true,
      includeTitleTags: true,
      headingsOffset  : -boardTop,
    }, CONFIG.toc));
    if (toc.find('.toc-list-item').length > 0) {
      toc.css('visibility', 'visible');
    }

    Fluid.events.registerRefreshCallback(function() {
      if ('tocbot' in window) {
        tocbot.refresh();
        var toc = jQuery('#toc');
        if (toc.length === 0 || !tocbot) {
          return;
        }
        if (toc.find('.toc-list-item').length > 0) {
          toc.css('visibility', 'visible');
        }
      }
    });
  });
</script>


  <script src=https://lib.baomitu.com/clipboard.js/2.0.11/clipboard.min.js></script>

  <script>Fluid.plugins.codeWidget();</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/anchor-js/4.3.1/anchor.min.js', function() {
    window.anchors.options = {
      placement: CONFIG.anchorjs.placement,
      visible  : CONFIG.anchorjs.visible
    };
    if (CONFIG.anchorjs.icon) {
      window.anchors.options.icon = CONFIG.anchorjs.icon;
    }
    var el = (CONFIG.anchorjs.element || 'h1,h2,h3,h4,h5,h6').split(',');
    var res = [];
    for (var item of el) {
      res.push('.markdown-body > ' + item.trim());
    }
    if (CONFIG.anchorjs.placement === 'left') {
      window.anchors.options.class = 'anchorjs-link-left';
    }
    window.anchors.add(res.join(', '));

    Fluid.events.registerRefreshCallback(function() {
      if ('anchors' in window) {
        anchors.removeAll();
        var el = (CONFIG.anchorjs.element || 'h1,h2,h3,h4,h5,h6').split(',');
        var res = [];
        for (var item of el) {
          res.push('.markdown-body > ' + item.trim());
        }
        if (CONFIG.anchorjs.placement === 'left') {
          anchors.options.class = 'anchorjs-link-left';
        }
        anchors.add(res.join(', '));
      }
    });
  });
</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.js', function() {
    Fluid.plugins.fancyBox();
  });
</script>


  <script>Fluid.plugins.imageCaption();</script>

  <script  src="/js/local-search.js" ></script>





<!-- 主题的启动项，将它保持在最底部 -->
<!-- the boot of the theme, keep it at the bottom -->
<script  src="/js/boot.js" ></script>


  

  <noscript>
    <div class="noscript-warning">博客在允许 JavaScript 运行的环境下浏览效果更佳</div>
  </noscript>
</body>
</html>
